﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ext.Net;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sio.Mdm.Entities;
using Sio.Mdm.BF.Ldap;
using Sio.Mdm.Types.Database;
using Sio.Mdm.Web.Controls;
using Sio.Mdm.Client;

namespace Sio.Mdm.Web.Instalacija
{
    public partial class LdapStreznikiPodrobno : Controls.Page
    {
        #region Properties
        int StreznikID
        {
            get
            {
                return Request.GetValue<int>("StreznikID");                
            }            
        }

        int OrgarnizacijaID
        {
            get
            {
                return Request.GetValue<int>("OrgarnizacijaID");
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && !X.IsAjaxRequest)
            {                
                if (StreznikID > 0)
                {
                    LoadStreznik(StreznikID);                    
                }
                LoadOrgarnizacije();
                LoadLdapTipi();
            }                        
        }
        
        private void SaveOrUpdate()         
        { 
            LdapStreznik streznik;

            if (StreznikID==0)
                streznik = EntityFactory.CreateInstance<LdapStreznik>();
            else
                streznik = EntityFactory.LoadEntity<LdapStreznik>(
                new Filter<LdapStreznik>().Where(r => r.LdapStreznikID == StreznikID));

                streznik.OrgarnizacijaID = Convert.ToInt32(cmbOrgarnizacije.SelectedItem.Value);
                streznik.Ime = txtIme.Text;
                streznik.Opis = txtOpis.Text;
                streznik.Naslov = txtIPNaslov.Text;                
                streznik.Tip = Convert.ToInt32(cmbTip.SelectedItem.Value);
                streznik.TipSinhronizacije = 0;                                         // Default for now                
                streznik.RootDN = txtRootDN.Text;
                streznik.Uporabnik = txtUporabnik.Text;
                streznik.Geslo = txtGeslo.Text;

                streznik.SslPovezava = chkSSLPovezava.Checked;
                //if (chkSSLPovezava.Checked) streznik.SslPovezava = true;

                if (StreznikID == 0)
                {
                    var slService = ServiceFactory.GetService<ILdapStreznikiService>();
                    slService.Create(streznik, this.UserSession);
                }
                else
                    streznik.Update();				
        }        

        private void LoadStreznik(int StreznikID)
        {
            LdapStreznik streznik = EntityFactory.LoadEntity<LdapStreznik>(
            new Filter<LdapStreznik>().Where(r => r.LdapStreznikID == StreznikID));

            txtIme.Text = streznik.Ime;
            txtOpis.Text = streznik.Opis;
            txtIPNaslov.Text = streznik.Naslov;
            
            cmbTip.SelectedItem.Value = streznik.Tip.ToString();
            cmbOrgarnizacije.SelectedItem.Value = streznik.OrgarnizacijaID.ToString();
            chkSSLPovezava.Checked = streznik.SslPovezava;            

            txtRootDN.Text = streznik.RootDN;
            txtUporabnik.Text = streznik.Uporabnik;
            txtGeslo.Text = streznik.Geslo;

            lblGesloHidden.Text = streznik.Geslo;   // TODO: This shoud be disabled on release version...!!

            cmbOrgarnizacije.Disabled = true;
        }

        private void LoadLdapTipi()
        {
            Dictionary<int, string> listTip = new Dictionary<int, string>();
            listTip.Add(0, "AD");
            listTip.Add(1, "LDAP");
            var tmpArray = (from p in listTip select new Object[2] { p.Key, p.Value }).ToArray();

            StoreLdapTipi.DataSource = tmpArray;
            StoreLdapTipi.DataBind();
        }

        private void LoadOrgarnizacije()
        {
            EntityCollection<Sio.Mdm.Types.Database.Organizacija> orgarnizacije = new EntityCollection<Sio.Mdm.Types.Database.Organizacija>(
                  new Filter<Sio.Mdm.Types.Database.Organizacija>().Where(o => o.OrganizacijaId != null).OrderBy(o => o.Naziv));
            StoreOrgarnizacije.DataSource = orgarnizacije;
            StoreOrgarnizacije.DataBind();
        }

        #region DirectMethods
        [DirectMethod]
        protected void ButtonClicked(object sender, DirectEventArgs e)
        {
            DialogButtons button = e.DeserializeParameter<DialogButtons>("buttonType");

            if (button == DialogButtons.Save)
            {
                SaveOrUpdate();
            }

            LdapStreznikPodrobnoProxy.Close();
        }        
        #endregion
    }
}
